home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / RCS_56.ARJ / RCSSYN.C < prev    next >
C/C++ Source or Header  |  1992-02-11  |  20KB  |  858 lines

  1. /*
  2.  *                     RCS file input
  3.  */
  4. /*********************************************************************************
  5.  *                       Syntax Analysis.
  6.  *                       Keyword table
  7.  *                       Testprogram: define SYNTEST
  8.  *                       Compatibility with Release 2: define COMPAT2=1
  9.  *********************************************************************************
  10.  */
  11.  
  12. /* Copyright (C) 1982, 1988, 1989 Walter Tichy
  13.    Copyright 1990, 1991 by Paul Eggert
  14.    Distributed under license by the Free Software Foundation, Inc.
  15.  
  16. This file is part of RCS.
  17.  
  18. RCS is free software; you can redistribute it and/or modify
  19. it under the terms of the GNU General Public License as published by
  20. the Free Software Foundation; either version 2, or (at your option)
  21. any later version.
  22.  
  23. RCS is distributed in the hope that it will be useful,
  24. but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  26. GNU General Public License for more details.
  27.  
  28. You should have received a copy of the GNU General Public License
  29. along with RCS; see the file COPYING.  If not, write to
  30. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  31.  
  32. Report problems and direct all questions to:
  33.  
  34.     rcs-bugs@cs.purdue.edu
  35.  
  36. */
  37.  
  38.  
  39. /* $Log: rcssyn.c,v $
  40.  * Revision 5.8  1991/08/19  03:13:55  eggert
  41.  * Tune.
  42.  *
  43.  * Revision 5.7  1991/04/21  11:58:29  eggert
  44.  * Disambiguate names on shortname hosts.
  45.  * Fix errno bug.  Add MS-DOS support.
  46.  *
  47.  * Revision 5.6  1991/02/28  19:18:51  eggert
  48.  * Fix null termination bug in reporting keyword expansion.
  49.  *
  50.  * Revision 5.5  1991/02/25  07:12:44  eggert
  51.  * Check diff output more carefully; avoid overflow.
  52.  *
  53.  * Revision 5.4  1990/11/01  05:28:48  eggert
  54.  * When ignoring unknown phrases, copy them to the output RCS file.
  55.  * Permit arbitrary data in logs and comment leaders.
  56.  * Don't check for nontext on initial checkin.
  57.  *
  58.  * Revision 5.3  1990/09/20  07:58:32  eggert
  59.  * Remove the test for non-text bytes; it caused more pain than it cured.
  60.  *
  61.  * Revision 5.2  1990/09/04  08:02:30  eggert
  62.  * Parse RCS files with no revisions.
  63.  * Don't strip leading white space from diff commands.  Count RCS lines better.
  64.  *
  65.  * Revision 5.1  1990/08/29  07:14:06  eggert
  66.  * Add -kkvl.  Clean old log messages too.
  67.  *
  68.  * Revision 5.0  1990/08/22  08:13:44  eggert
  69.  * Try to parse future RCS formats without barfing.
  70.  * Add -k.  Don't require final newline.
  71.  * Remove compile-time limits; use malloc instead.
  72.  * Don't output branch keyword if there's no default branch,
  73.  * because RCS version 3 doesn't understand it.
  74.  * Tune.  Remove lint.
  75.  * Add support for ISO 8859.  Ansify and Posixate.
  76.  * Check that a newly checked-in file is acceptable as input to 'diff'.
  77.  * Check diff's output.
  78.  *
  79.  * Revision 4.6  89/05/01  15:13:32  narten
  80.  * changed copyright header to reflect current distribution rules
  81.  * 
  82.  * Revision 4.5  88/08/09  19:13:21  eggert
  83.  * Allow cc -R; remove lint.
  84.  * 
  85.  * Revision 4.4  87/12/18  11:46:16  narten
  86.  * more lint cleanups (Guy Harris)
  87.  * 
  88.  * Revision 4.3  87/10/18  10:39:36  narten
  89.  * Updating version numbers. Changes relative to 1.1 actually relative to
  90.  * 4.1
  91.  * 
  92.  * Revision 1.3  87/09/24  14:00:49  narten
  93.  * Sources now pass through lint (if you ignore printf/sprintf/fprintf 
  94.  * warnings)
  95.  * 
  96.  * Revision 1.2  87/03/27  14:22:40  jenkins
  97.  * Port to suns
  98.  * 
  99.  * Revision 4.1  83/03/28  11:38:49  wft
  100.  * Added parsing and printing of default branch.
  101.  * 
  102.  * Revision 3.6  83/01/15  17:46:50  wft
  103.  * Changed readdelta() to initialize selector and log-pointer.
  104.  * Changed puttree to check for selector==DELETE; putdtext() uses DELNUMFORM.
  105.  *
  106.  * Revision 3.5  82/12/08  21:58:58  wft
  107.  * renamed Commentleader to Commleader.
  108.  *
  109.  * Revision 3.4  82/12/04  13:24:40  wft
  110.  * Added routine gettree(), which updates keeplock after reading the
  111.  * delta tree.
  112.  *
  113.  * Revision 3.3  82/11/28  21:30:11  wft
  114.  * Reading and printing of Suffix removed; version COMPAT2 skips the
  115.  * Suffix for files of release 2 format. Fixed problems with printing nil.
  116.  *
  117.  * Revision 3.2  82/10/18  21:18:25  wft
  118.  * renamed putdeltatext to putdtext.
  119.  *
  120.  * Revision 3.1  82/10/11  19:45:11  wft
  121.  * made sure getc() returns into an integer.
  122.  */
  123.  
  124.  
  125.  
  126. /* version COMPAT2 reads files of the format of release 2 and 3, but
  127.  * generates files of release 3 format. Need not be defined if no
  128.  * old RCS files generated with release 2 exist.
  129.  */
  130. /* version SYNTEST inputs a RCS file and then prints out its internal
  131.  * data structures.
  132. */
  133.  
  134. #include "rcsbase.h"
  135.  
  136. libId(synId, "$Id: rcssyn.c,v 5.8 1991/08/19 03:13:55 eggert Exp $")
  137.  
  138. /* forward */
  139. static char const *getkeyval P((char const*,enum tokens,int));
  140. static int strn2expmode P((char const*,size_t));
  141.  
  142. /* keyword table */
  143.  
  144. char const
  145.     Kdesc[]     = "desc",
  146.     Klog[]      = "log",
  147.     Ktext[]     = "text";
  148.  
  149. static char const
  150.     Kaccess[]   = "access",
  151.     Kauthor[]   = "author",
  152.     Kbranch[]   = "branch",
  153.     K_branches[]= "branches",
  154.     Kcomment[]  = "comment",
  155.     Kdate[]     = "date",
  156.     Kexpand[]   = "expand",
  157.     Khead[]     = "head",
  158.     Klocks[]    = "locks",
  159.     Knext[]     = "next",
  160.     Kstate[]    = "state",
  161.     Kstrict[]   = "strict",
  162. #if COMPAT2
  163.     Ksuffix[]   = "suffix",
  164. #endif
  165.     Ksymbols[]  = "symbols";
  166.  
  167. static struct buf Commleader;
  168. static struct cbuf Ignored;
  169. struct cbuf Comment;
  170. struct access   * AccessList;
  171. struct assoc    * Symbols;
  172. struct lock     * Locks;
  173. int          Expand;
  174. int               StrictLocks;
  175. struct hshentry * Head;
  176. char const      * Dbranch;
  177. unsigned TotalDeltas;
  178.  
  179.  
  180.     static void
  181. getsemi(key)
  182.     char const *key;
  183. /* Get a semicolon to finish off a phrase started by KEY.  */
  184. {
  185.     if (!getlex(SEMI))
  186.         fatserror("missing ';' after '%s'", key);
  187. }
  188.  
  189.     static struct hshentry *
  190. getdnum()
  191. /* Get a delta number.  */
  192. {
  193.     register struct hshentry *delta = getnum();
  194.     if (delta && countnumflds(delta->num)&1)
  195.         fatserror("%s isn't a delta number", delta->num);
  196.     return delta;
  197. }
  198.  
  199.  
  200.     void
  201. getadmin()
  202. /* Read an <admin> and initialize the appropriate global variables.  */
  203. {
  204.     register char const *id;
  205.         struct access   * newaccess;
  206.         struct assoc    * newassoc;
  207.         struct lock     * newlock;
  208.         struct hshentry * delta;
  209.     struct access **LastAccess;
  210.     struct assoc **LastSymbol;
  211.     struct lock **LastLock;
  212.     struct buf b;
  213.     struct cbuf cb;
  214.  
  215.         TotalDeltas=0;
  216.  
  217.     getkey(Khead);
  218.     Head = getdnum();
  219.     getsemi(Khead);
  220.  
  221.     Dbranch = nil;
  222.     if (getkeyopt(Kbranch)) {
  223.         if ((delta = getnum()))
  224.             Dbranch = delta->num;
  225.         getsemi(Kbranch);
  226.         }
  227.  
  228.  
  229. #if COMPAT2
  230.         /* read suffix. Only in release 2 format */
  231.     if (getkeyopt(Ksuffix)) {
  232.                 if (nexttok==STRING) {
  233.             readstring(); nextlex(); /* Throw away the suffix.  */
  234.         } else if (nexttok==ID) {
  235.                         nextlex();
  236.                 }
  237.         getsemi(Ksuffix);
  238.         }
  239. #endif
  240.  
  241.     getkey(Kaccess);
  242.     LastAccess = &AccessList;
  243.         while (id=getid()) {
  244.         newaccess = ftalloc(struct access);
  245.                 newaccess->login = id;
  246.         *LastAccess = newaccess;
  247.         LastAccess = &newaccess->nextaccess;
  248.         }
  249.     *LastAccess = nil;
  250.     getsemi(Kaccess);
  251.  
  252.     getkey(Ksymbols);
  253.     LastSymbol = &Symbols;
  254.         while (id = getid()) {
  255.                 if (!getlex(COLON))
  256.             fatserror("missing ':' in symbolic name definition");
  257.                 if (!(delta=getnum())) {
  258.             fatserror("missing number in symbolic name definition");
  259.                 } else { /*add new pair to association list*/
  260.             newassoc = ftalloc(struct assoc);
  261.                         newassoc->symbol=id;
  262.             newassoc->num = delta->num;
  263.             *LastSymbol = newassoc;
  264.             LastSymbol = &newassoc->nextassoc;
  265.                 }
  266.         }
  267.     *LastSymbol = nil;
  268.     getsemi(Ksymbols);
  269.  
  270.     getkey(Klocks);
  271.     LastLock = &Locks;
  272.         while (id = getid()) {
  273.                 if (!getlex(COLON))
  274.             fatserror("missing ':' in lock");
  275.         if (!(delta=getdnum())) {
  276.             fatserror("missing number in lock");
  277.                 } else { /*add new pair to lock list*/
  278.             newlock = ftalloc(struct lock);
  279.                         newlock->login=id;
  280.                         newlock->delta=delta;
  281.             *LastLock = newlock;
  282.             LastLock = &newlock->nextlock;
  283.                 }
  284.         }
  285.     *LastLock = nil;
  286.     getsemi(Klocks);
  287.  
  288.     if ((StrictLocks = getkeyopt(Kstrict)))
  289.         getsemi(Kstrict);
  290.  
  291.     Comment.size = 0;
  292.     if (getkeyopt(Kcomment)) {
  293.         if (nexttok==STRING) {
  294.             Comment = savestring(&Commleader);
  295.             nextlex();
  296.         }
  297.         getsemi(Kcomment);
  298.         }
  299.  
  300.     Expand = KEYVAL_EXPAND;
  301.     if (getkeyopt(Kexpand)) {
  302.         if (nexttok==STRING) {
  303.             bufautobegin(&b);
  304.             cb = savestring(&b);
  305.             if ((Expand = strn2expmode(cb.string,cb.size)) < 0)
  306.                 fatserror("unknown expand mode %.*s",
  307.                 (int)cb.size, cb.string
  308.                 );
  309.             bufautoend(&b);
  310.             nextlex();
  311.         }
  312.         getsemi(Kexpand);
  313.         }
  314.     Ignored = getphrases(Kdesc);
  315. }
  316.  
  317. char const *const expand_names[] = {
  318.     /* These must agree with *_EXPAND in rcsbase.h.  */
  319.     "kv","kvl","k","v","o",
  320.     0
  321. };
  322.  
  323.     int
  324. str2expmode(s)
  325.     char const *s;
  326. /* Yield expand mode corresponding to S, or -1 if bad.  */
  327. {
  328.     return strn2expmode(s, strlen(s));
  329. }
  330.  
  331.     static int
  332. strn2expmode(s, n)
  333.     char const *s;
  334.     size_t n;
  335. {
  336.     char const *const *p;
  337.  
  338.     for (p = expand_names;  *p;  ++p)
  339.         if (memcmp(*p,s,n) == 0  &&  !(*p)[n])
  340.             return p - expand_names;
  341.     return -1;
  342. }
  343.  
  344.  
  345.     void
  346. ignorephrase()
  347. /* Ignore a phrase introduced by a later version of RCS.  */
  348. {
  349.     warnignore();
  350.     hshenter=false;
  351.     for (;;) {
  352.         switch (nexttok) {
  353.         case SEMI: hshenter=true; nextlex(); return;
  354.         case ID:
  355.         case NUM: ffree1(NextString); break;
  356.         case STRING: readstring(); break;
  357.         default: break;
  358.         }
  359.         nextlex();
  360.     }
  361. }
  362.  
  363.  
  364.     static int
  365. getdelta()
  366. /* Function: reads a delta block.
  367.  * returns false if the current block does not start with a number.
  368.  */
  369. {
  370.         register struct hshentry * Delta, * num;
  371.     struct branchhead **LastBranch, *NewBranch;
  372.  
  373.     if (!(Delta = getdnum()))
  374.         return false;
  375.  
  376.         hshenter = false; /*Don't enter dates into hashtable*/
  377.         Delta->date = getkeyval(Kdate, NUM, false);
  378.         hshenter=true;    /*reset hshenter for revision numbers.*/
  379.  
  380.         Delta->author = getkeyval(Kauthor, ID, false);
  381.  
  382.         Delta->state = getkeyval(Kstate, ID, true);
  383.  
  384.     getkey(K_branches);
  385.     LastBranch = &Delta->branches;
  386.     while ((num = getdnum())) {
  387.         NewBranch = ftalloc(struct branchhead);
  388.                 NewBranch->hsh = num;
  389.         *LastBranch = NewBranch;
  390.         LastBranch = &NewBranch->nextbranch;
  391.         }
  392.     *LastBranch = nil;
  393.     getsemi(K_branches);
  394.  
  395.     getkey(Knext);
  396.     Delta->next = num = getdnum();
  397.     getsemi(Knext);
  398.     Delta->lockedby = nil;
  399.     Delta->log.string = 0;
  400.     Delta->selector = true;
  401.     Delta->ig = getphrases(Kdesc);
  402.         TotalDeltas++;
  403.         return (true);
  404. }
  405.  
  406.  
  407.     void
  408. gettree()
  409. /* Function: Reads in the delta tree with getdelta(), then
  410.  * updates the lockedby fields.
  411.  */
  412. {
  413.     struct lock const *currlock;
  414.  
  415.         while (getdelta());
  416.         currlock=Locks;
  417.         while (currlock) {
  418.                 currlock->delta->lockedby = currlock->login;
  419.                 currlock = currlock->nextlock;
  420.         }
  421. }
  422.  
  423.  
  424.     void
  425. getdesc(prdesc)
  426. int  prdesc;
  427. /* Function: read in descriptive text
  428.  * nexttok is not advanced afterwards.
  429.  * If prdesc is set, the text is printed to stdout.
  430.  */
  431. {
  432.  
  433.     getkeystring(Kdesc);
  434.         if (prdesc)
  435.                 printstring();  /*echo string*/
  436.         else    readstring();   /*skip string*/
  437. }
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444.     static char const *
  445. getkeyval(keyword, token, optional)
  446.     char const *keyword;
  447.     enum tokens token;
  448.     int optional;
  449. /* reads a pair of the form
  450.  * <keyword> <token> ;
  451.  * where token is one of <id> or <num>. optional indicates whether
  452.  * <token> is optional. A pointer to
  453.  * the actual character string of <id> or <num> is returned.
  454.  */
  455. {
  456.     register char const *val = nil;
  457.  
  458.     getkey(keyword);
  459.         if (nexttok==token) {
  460.                 val = NextString;
  461.                 nextlex();
  462.         } else {
  463.         if (!optional)
  464.             fatserror("missing %s", keyword);
  465.         }
  466.     getsemi(keyword);
  467.         return(val);
  468. }
  469.  
  470.  
  471.  
  472.  
  473.     void
  474. putadmin(fout)
  475. register FILE * fout;
  476. /* Function: Print the <admin> node read with getadmin() to file fout.
  477.  * Assumption: Variables AccessList, Symbols, Locks, StrictLocks,
  478.  * and Head have been set.
  479.  */
  480. {
  481.     struct assoc const *curassoc;
  482.     struct lock const *curlock;
  483.     struct access const *curaccess;
  484.  
  485.     aprintf(fout, "%s\t%s;\n", Khead, Head?Head->num:"");
  486.     if (Dbranch && VERSION(4)<=RCSversion)
  487.         aprintf(fout, "%s\t%s;\n", Kbranch, Dbranch);
  488.  
  489.     aputs(Kaccess, fout);
  490.         curaccess = AccessList;
  491.         while (curaccess) {
  492.            aprintf(fout, "\n\t%s", curaccess->login);
  493.                curaccess = curaccess->nextaccess;
  494.         }
  495.     aprintf(fout, ";\n%s", Ksymbols);
  496.         curassoc = Symbols;
  497.         while (curassoc) {
  498.            aprintf(fout, "\n\t%s:%s", curassoc->symbol, curassoc->num);
  499.                curassoc = curassoc->nextassoc;
  500.         }
  501.     aprintf(fout, ";\n%s", Klocks);
  502.         curlock = Locks;
  503.         while (curlock) {
  504.            aprintf(fout, "\n\t%s:%s", curlock->login, curlock->delta->num);
  505.                curlock = curlock->nextlock;
  506.         }
  507.     if (StrictLocks) aprintf(fout, "; %s", Kstrict);
  508.     aprintf(fout, ";\n");
  509.     if (Comment.size) {
  510.         aprintf(fout, "%s\t", Kcomment);
  511.         putstring(fout, true, Comment, false);
  512.         aprintf(fout, ";\n");
  513.         }
  514.     if (Expand != KEYVAL_EXPAND)
  515.         aprintf(fout, "%s\t%c%s%c;\n",
  516.             Kexpand, SDELIM, expand_names[Expand], SDELIM
  517.         );
  518.     awrite(Ignored.string, Ignored.size, fout);
  519.     aputc('\n', fout);
  520. }
  521.  
  522.  
  523.  
  524.  
  525.     static void
  526. putdelta(node,fout)
  527. register struct hshentry const *node;
  528. register FILE * fout;
  529. /* Function: prints a <delta> node to fout;
  530.  */
  531. {
  532.     struct branchhead const *nextbranch;
  533.  
  534.         if (node == nil) return;
  535.  
  536.     aprintf(fout, "\n%s\n%s\t%s;\t%s %s;\t%s %s;\nbranches",
  537.         node->num,
  538.         Kdate, node->date,
  539.         Kauthor, node->author,
  540.         Kstate, node->state?node->state:""
  541.     );
  542.         nextbranch = node->branches;
  543.         while (nextbranch) {
  544.            aprintf(fout, "\n\t%s", nextbranch->hsh->num);
  545.                nextbranch = nextbranch->nextbranch;
  546.         }
  547.  
  548.     aprintf(fout, ";\n%s\t%s;\n", Knext, node->next?node->next->num:"");
  549.     awrite(node->ig.string, node->ig.size, fout);
  550. }
  551.  
  552.  
  553.  
  554.  
  555.     void
  556. puttree(root,fout)
  557. struct hshentry const *root;
  558. register FILE * fout;
  559. /* Function: prints the delta tree in preorder to fout, starting with root.
  560.  */
  561. {
  562.     struct branchhead const *nextbranch;
  563.  
  564.         if (root==nil) return;
  565.  
  566.     if (root->selector)
  567.         putdelta(root,fout);
  568.  
  569.         puttree(root->next,fout);
  570.  
  571.         nextbranch = root->branches;
  572.         while (nextbranch) {
  573.              puttree(nextbranch->hsh,fout);
  574.              nextbranch = nextbranch->nextbranch;
  575.         }
  576. }
  577.  
  578.  
  579.     static exiting void
  580. unexpected_EOF()
  581. {
  582.     faterror("unexpected EOF in diff output");
  583. }
  584.  
  585. int putdtext(num,log,srcfilename,fout,diffmt)
  586.     char const *num, *srcfilename;
  587.     struct cbuf log;
  588.     FILE *fout;
  589.     int diffmt;
  590. /* Function: write a deltatext-node to fout.
  591.  * num points to the deltanumber, log to the logmessage, and
  592.  * sourcefile contains the text. Doubles up all SDELIMs in both the
  593.  * log and the text; Makes sure the log message ends in \n.
  594.  * returns false on error.
  595.  * If diffmt is true, also checks that text is valid diff -n output.
  596.  */
  597. {
  598.     RILE *fin;
  599.     int result;
  600.     if (!(fin = Iopen(srcfilename, "r", (struct stat*)0))) {
  601.         eerror(srcfilename);
  602.         return false;
  603.     }
  604.     result = putdftext(num,log,fin,fout,diffmt);
  605.     Ifclose(fin);
  606.     return result;
  607. }
  608.  
  609.     void
  610. putstring(out, delim, s, log)
  611.     register FILE *out;
  612.     struct cbuf s;
  613.     int delim, log;
  614. /*
  615.  * Output to OUT one SDELIM if DELIM, then the string S with SDELIMs doubled.
  616.  * If LOG is set then S is a log string; append a newline if S is nonempty.
  617.  */
  618. {
  619.     register char const *sp;
  620.     register size_t ss;
  621.  
  622.     if (delim)
  623.         aputc(SDELIM, out);
  624.     sp = s.string;
  625.     for (ss = s.size;  ss;  --ss) {
  626.         if (*sp == SDELIM)
  627.             aputc(SDELIM, out);
  628.         aputc(*sp++, out);
  629.     }
  630.     if (s.size && log)
  631.         aputc('\n', out);
  632.     aputc(SDELIM, out);
  633. }
  634.  
  635.     int
  636. putdftext(num,log,finfile,foutfile,diffmt)
  637.     char const *num;
  638.     struct cbuf log;
  639.     RILE *finfile;
  640.     FILE *foutfile;
  641.     int diffmt;
  642. /* like putdtext(), except the source file is already open */
  643. {
  644.     declarecache;
  645.     register FILE *fout;
  646.     register int c;
  647.     register RILE *fin;
  648.     int ed;
  649.     struct diffcmd dc;
  650.  
  651.     fout = foutfile;
  652.     aprintf(fout,DELNUMFORM,num,Klog);
  653.         /* put log */
  654.     putstring(fout, true, log, true);
  655.         /* put text */
  656.     aprintf(fout, "\n%s\n%c", Ktext, SDELIM);
  657.     fin = finfile;
  658.     setupcache(fin);
  659.     if (!diffmt) {
  660.         /* Copy the file */
  661.         cache(fin);
  662.         for (;;) {
  663.         cachegeteof(c, break;);
  664.         if (c==SDELIM) aputc(SDELIM,fout);   /*double up SDELIM*/
  665.         aputc(c,fout);
  666.         }
  667.     } else {
  668.         initdiffcmd(&dc);
  669.         while (0  <=  (ed = getdiffcmd(fin,false,fout,&dc)))
  670.         if (ed) {
  671.             cache(fin);
  672.             while (dc.nlines--)
  673.             do {
  674.                 cachegeteof(c, { if (!dc.nlines) goto OK_EOF; unexpected_EOF(); });
  675.                 if (c == SDELIM)
  676.                 aputc(SDELIM,fout);
  677.                 aputc(c,fout);
  678.             } while (c != '\n');
  679.             uncache(fin);
  680.         }
  681.     }
  682.     OK_EOF:
  683.     aprintf(fout, "%c\n", SDELIM);
  684.     return true;
  685. }
  686.  
  687.     void
  688. initdiffcmd(dc)
  689.     register struct diffcmd *dc;
  690. /* Initialize *dc suitably for getdiffcmd(). */
  691. {
  692.     dc->adprev = 0;
  693.     dc->dafter = 0;
  694. }
  695.  
  696.     static exiting void
  697. badDiffOutput(buf)
  698.     char const *buf;
  699. {
  700.     faterror("bad diff output line: %s", buf);
  701. }
  702.  
  703.     static exiting void
  704. diffLineNumberTooLarge(buf)
  705.     char const *buf;
  706. {
  707.     faterror("diff line number too large: %s", buf);
  708. }
  709.  
  710.     int
  711. getdiffcmd(finfile, delimiter, foutfile, dc)
  712.     RILE *finfile;
  713.     FILE *foutfile;
  714.     int delimiter;
  715.     struct diffcmd *dc;
  716. /* Get a editing command output by 'diff -n' from fin.
  717.  * The input is delimited by SDELIM if delimiter is set, EOF otherwise.
  718.  * Copy a clean version of the command to fout (if nonnull).
  719.  * Yield 0 for 'd', 1 for 'a', and -1 for EOF.
  720.  * Store the command's line number and length into dc->line1 and dc->nlines.
  721.  * Keep dc->adprev and dc->dafter up to date.
  722.  */
  723. {
  724.     register int c;
  725.     declarecache;
  726.     register FILE *fout;
  727.     register char *p;
  728.     register RILE *fin;
  729.     unsigned long line1, nlines, t;
  730.     char buf[BUFSIZ];
  731.  
  732.     fin = finfile;
  733.     fout = foutfile;
  734.     setupcache(fin); cache(fin);
  735.     cachegeteof(c, { if (delimiter) unexpected_EOF(); return -1; } );
  736.     if (delimiter) {
  737.         if (c==SDELIM) {
  738.             cacheget(c);
  739.             if (c==SDELIM) {
  740.                 buf[0] = c;
  741.                 buf[1] = 0;
  742.                 badDiffOutput(buf);
  743.             }
  744.             uncache(fin);
  745.             nextc = c;
  746.             if (fout)
  747.                 aprintf(fout, "%c%c", SDELIM, c);
  748.             return -1;
  749.         }
  750.     }
  751.     p = buf;
  752.     do {
  753.         if (buf+BUFSIZ-2 <= p) {
  754.             faterror("diff output command line too long");
  755.         }
  756.         *p++ = c;
  757.         cachegeteof(c, unexpected_EOF();) ;
  758.     } while (c != '\n');
  759.     uncache(fin);
  760.     if (delimiter)
  761.         ++rcsline;
  762.     *p = '\0';
  763.     for (p = buf+1;  (c = *p++) == ' ';  )
  764.         ;
  765.     line1 = 0;
  766.     while (isdigit(c)) {
  767.         t = line1 * 10;
  768.         if (
  769.             ULONG_MAX/10 < line1  ||
  770.             (line1 = t + (c - '0'))  <  t
  771.         )
  772.             diffLineNumberTooLarge(buf);
  773.         c = *p++;
  774.     }
  775.     while (c == ' ')
  776.         c = *p++;
  777.     nlines = 0;
  778.     while (isdigit(c)) {
  779.         t = nlines * 10;
  780.         if (
  781.             ULONG_MAX/10 < nlines  ||
  782.             (nlines = t + (c - '0'))  <  t
  783.         )
  784.             diffLineNumberTooLarge(buf);
  785.         c = *p++;
  786.     }
  787.     if (c || !nlines) {
  788.         badDiffOutput(buf);
  789.     }
  790.     if (line1+nlines < line1)
  791.         diffLineNumberTooLarge(buf);
  792.     switch (buf[0]) {
  793.         case 'a':
  794.         if (line1 < dc->adprev) {
  795.             faterror("backward insertion in diff output: %s", buf);
  796.         }
  797.         dc->adprev = line1 + 1;
  798.         break;
  799.         case 'd':
  800.         if (line1 < dc->adprev  ||  line1 < dc->dafter) {
  801.             faterror("backward deletion in diff output: %s", buf);
  802.         }
  803.         dc->adprev = line1;
  804.         dc->dafter = line1 + nlines;
  805.         break;
  806.         default:
  807.         badDiffOutput(buf);
  808.     }
  809.     if (fout) {
  810.         aprintf(fout, "%s\n", buf);
  811.     }
  812.     dc->line1 = line1;
  813.     dc->nlines = nlines;
  814.     return buf[0] == 'a';
  815. }
  816.  
  817.  
  818.  
  819. #ifdef SYNTEST
  820.  
  821. char const cmdid[] = "syntest";
  822.  
  823.     int
  824. main(argc,argv)
  825. int argc; char * argv[];
  826. {
  827.  
  828.         if (argc<2) {
  829.         aputs("No input file\n",stderr);
  830.         exitmain(EXIT_FAILURE);
  831.         }
  832.     if (!(finptr = Iopen(argv[1], FOPEN_R, (struct stat*)0))) {
  833.         faterror("can't open input file %s", argv[1]);
  834.         }
  835.         Lexinit();
  836.         getadmin();
  837.         putadmin(stdout);
  838.  
  839.         gettree();
  840.         puttree(Head,stdout);
  841.  
  842.         getdesc(true);
  843.  
  844.     nextlex();
  845.  
  846.     if (!eoflex()) {
  847.         fatserror("expecting EOF");
  848.         }
  849.     exitmain(EXIT_SUCCESS);
  850. }
  851.  
  852.  
  853. exiting void exiterr() { _exit(EXIT_FAILURE); }
  854.  
  855.  
  856. #endif
  857.  
  858.